<?php

namespace JLPay\Models\Openmerch\Register;

/**
 * 审核详情
 */
class AuditDetail
{
    protected ?string $module = null;
    protected ?string $moduleName = null;
    protected ?string $failedReason = null;
    protected ?array $moduleDetail = null;

    /**
     * 构造函数
     *
     * @param string|null $module
     * @param string|null $moduleName
     * @param string|null $failedReason
     * @param array|null $moduleDetail
     */
    public function __construct(?string $module = null, ?string $moduleName = null, ?string $failedReason = null, ?array $moduleDetail = null)
    {
        $this->module = $module;
        $this->moduleName = $moduleName;
        $this->failedReason = $failedReason;
        $this->moduleDetail = $moduleDetail ?? [];
    }

    /**
     * 设置模块变量名
     *
     * @param string $module
     * @return self
     */
    public function setModule(string $module): self
    {
        $this->module = $module;
        return $this;
    }

    /**
     * 设置模块中文名
     *
     * @param string $moduleName
     * @return self
     */
    public function setModuleName(string $moduleName): self
    {
        $this->moduleName = $moduleName;
        return $this;
    }

    /**
     * 设置模块审核不通过原因
     *
     * @param string $failedReason
     * @return self
     */
    public function setFailedReason(string $failedReason): self
    {
        $this->failedReason = $failedReason;
        return $this;
    }

    /**
     * 设置字段审核不通过原因列表
     *
     * @param array $moduleDetail
     * @return self
     */
    public function setModuleDetail(array $moduleDetail): self
    {
        $this->moduleDetail = $moduleDetail;
        return $this;
    }

    /**
     * 获取模块变量名
     *
     * @return string|null
     */
    public function getModule(): ?string
    {
        return $this->module;
    }

    /**
     * 获取模块中文名
     *
     * @return string|null
     */
    public function getModuleName(): ?string
    {
        return $this->moduleName;
    }

    /**
     * 获取模块审核不通过原因
     *
     * @return string|null
     */
    public function getFailedReason(): ?string
    {
        return $this->failedReason;
    }

    /**
     * 获取字段审核不通过原因列表
     *
     * @return array
     */
    public function getModuleDetail(): array
    {
        return $this->moduleDetail ?? [];
    }

    /**
     * 转换为数组
     *
     * @return array
     */
    public function toArray(): array
    {
        $moduleDetailArray = [];
        if ($this->moduleDetail) {
            foreach ($this->moduleDetail as $detail) {
                if ($detail instanceof FieldDetail) {
                    $moduleDetailArray[] = $detail->toArray();
                } else {
                    $moduleDetailArray[] = $detail;
                }
            }
        }

        return [
            'module' => $this->module,
            'module_name' => $this->moduleName,
            'failed_reason' => $this->failedReason,
            'module_detail' => $moduleDetailArray
        ];
    }
} 